package dxn.nl.file.client.core;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

@Component
public class CsExportManager implements ApplicationContextAware {

    private static final Map<String, DealExportService> exportServerTypeMap = new HashMap<>();
    private static final Map<String, DealImportService> uploadServerTypeMap = new HashMap<>();

    private static ReentrantLock lockBasicSite = new ReentrantLock(true);
    private static ReentrantLock lockExportService = new ReentrantLock(true);

    private static ApplicationContext applicationContext;

    /**
     * 服务器启动，Spring容器初始化时，当加载了当前类为bean组件后，
     * 将会调用下面方法注入ApplicationContext实例
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("初始化了");
        CsExportManager.applicationContext = applicationContext;

        getExportDealService("1");
        getUploadDealService("1");
    }


    public static DealExportService getExportDealService(String key)
    {
        if(exportServerTypeMap.size() == 0)
        {
            initExportServer();
        }
        return exportServerTypeMap.get(key);
    }

    public static DealImportService getUploadDealService(String key)
    {
        if(uploadServerTypeMap.size() == 0)
        {
            initUploadServer();
        }
        return uploadServerTypeMap.get(key);
    }

    private static void initUploadServer()
    {
        try {
            boolean lock = lockBasicSite.tryLock(10, TimeUnit.SECONDS);
            if(lock)
            {
                if(uploadServerTypeMap.size() == 0)
                {
                    String[] beanNames = applicationContext.getBeanNamesForType(DealImportService.class);
                    for(String beanName : beanNames)
                    {
                        DealImportService importService = (DealImportService)applicationContext.getBean(beanName);

                        Class<?> c = importService.getClass();
                        // 获取类注解
                        CsExportDealServer dataSourceAnnotation = c.getAnnotation(CsExportDealServer.class);
                        if(dataSourceAnnotation != null) {
                            String[] valueArray = dataSourceAnnotation.value();
                            if(valueArray != null && valueArray.length !=0)
                            {
                                for(String value:valueArray)
                                {
                                    uploadServerTypeMap.put(value,importService);
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {

        }
        finally {
            lockBasicSite.unlock();
        }
    }

    private static void initExportServer()
    {
        try {
            boolean lock = lockExportService.tryLock(10, TimeUnit.SECONDS);
            if(lock)
            {
                if(exportServerTypeMap.size() == 0)
                {
                    String[] beanNames = applicationContext.getBeanNamesForType(DealExportService.class);
                    for(String beanName : beanNames)
                    {
                        DealExportService importService = (DealExportService)applicationContext.getBean(beanName);

                        Class<?> c = importService.getClass();
                        // 获取类注解
                        CsExportDealServer dataSourceAnnotation = c.getAnnotation(CsExportDealServer.class);
                        if(dataSourceAnnotation != null) {
                            String[] valueArray = dataSourceAnnotation.value();
                            if(valueArray != null && valueArray.length !=0)
                            {
                                for(String value:valueArray)
                                {
                                    exportServerTypeMap.put(value,importService);
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {

        }
        finally {
            lockExportService.unlock();
        }
    }


}
